from django.db import models

from apps.users.models import BaseModel

# Create your models here.

# 机构类别选择
CATEGORY_CHOICES = (
    ("institution", "培训机构"),
    ("individual", "个人讲师"),
    ("school", "学校"),

)


# 城市信息表
class City(BaseModel):
    # 城市名称
    name = models.CharField(verbose_name=u"城市名称", max_length=20)
    # 城市描述
    desc = models.CharField(verbose_name=u"城市描述", max_length=200)

    # 配置信息
    class Meta:
        verbose_name = "城市信息"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 授课机构表
class CourseOrg(BaseModel):
    # 机构名称
    name = models.CharField(verbose_name="机构名称", max_length=50)
    # 机构描述
    desc = models.TextField(verbose_name="机构描述")
    # 机构标签
    tag = models.CharField(verbose_name="机构标签", max_length=10, default="普通")
    # 机构类别
    category = models.CharField(verbose_name="机构类别", max_length=11, choices=CATEGORY_CHOICES, default="培训机构")
    # 点击数
    click_nums = models.IntegerField(verbose_name="点击数", default=0)
    # 收藏数
    fav_nums = models.IntegerField(verbose_name="收藏数", default=0)
    # 机构Logo
    org_logo = models.ImageField(verbose_name="机构Logo", upload_to="org_logo/%Y%m", max_length=100)
    # 机构地址
    address = models.CharField(verbose_name="机构地址", max_length=150)
    # 学习人数
    students = models.IntegerField(verbose_name="学习人数", default=0)
    # 课程数
    course_num = models.IntegerField(verbose_name="课程数", default=0)
    # 所在城市
    city = models.ForeignKey(City, verbose_name="所在城市", on_delete=models.CASCADE)

    # 获取课程
    def courses(self):
        courses = self.course_set.filter(is_classic=True)[:2]
        return courses

    # 配置信息
    class Meta:
        verbose_name = "授课机构"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 老师信息表
class Teacher(BaseModel):
    # 授课机构
    org = models.ForeignKey(CourseOrg, verbose_name="授课机构", on_delete=models.CASCADE)
    # 姓名
    name = models.CharField(verbose_name=u"教师姓名", max_length=50)
    # 工作年限
    work_years = models.IntegerField(verbose_name="工作年限", default=0)
    # 就职公司
    work_company = models.CharField(verbose_name="就职公司", max_length=50)
    # 公司职位
    work_position = models.CharField(verbose_name="公司职位", max_length=50)
    # 教学特点
    characteristic = models.CharField(verbose_name="教学特点", max_length=50)
    # 年龄
    age = models.IntegerField(verbose_name="年龄", default=0)
    # 点击数
    click_nums = models.IntegerField(verbose_name="点击数", default=0)
    # 收藏数
    fav_nums = models.IntegerField(verbose_name="收藏数", default=0)
    # 头像
    avatar = models.ImageField(verbose_name="头像", upload_to="avatar/teacher/%Y/%m",
                               default="avatar/default/default_avatar.png")

    # 配置信息
    class Meta:
        verbose_name = "教师"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    def course_nums(self):
        return self.course_set.all().count()
